home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 446 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  177 lines

  1. Path: ix.netcom.com!netnews
  2. From: nanninr@ix.netcom.com(Robert A. Nannini )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Using BC4.5 TArrayAsVector of my class in a class!
  5. Date: 4 Jan 1996 16:24:55 GMT
  6. Organization: Netcom
  7. Message-ID: <4cgv0n$djd@ixnews8.ix.netcom.com>
  8. References: <DKMHJE.B0B@westminster.ac.uk>
  9. NNTP-Posting-Host: ix-stl4-18.ix.netcom.com
  10. X-NETCOM-Date: Thu Jan 04  8:24:55 AM PST 1996
  11.  
  12. In <DKMHJE.B0B@westminster.ac.uk> besec@wmin.ac.uk (Nicholas Arthur
  13. Llewellyn) writes: 
  14. >
  15. >This is my first use of BC4.5 and I'm trying to use the array class
  16. >library to hold the class Card, in the class's Pack and Hand.
  17. >
  18. >I get the error:
  19. >
  20. >Compiling PROJECT.CPP:
  21. >
  22. ><This is the one I'm trying to fix>
  23. >Error PROJECT.CPP 71: Cannot find default constructor to initialize
  24. >member 'pack' in function Pack::Pack()
  25. >
  26. ><These other two I got from messing around trying to fix the above
  27. >error>
  28. >Error PROJECT.CPP 72: Type qualifier 'pack' must be a struct or class
  29. >name in function Pack::Pack()
  30. >Error PROJECT.CPP 72: Statement missing ; in function Pack::Pack()
  31. >
  32. >The Borland manuals are not much help, all the examples are too
  33. >simple.
  34. >
  35. >Is there any one out there who can point in the correct direction?
  36. >
  37. >Here's the code:
  38. >
  39. >// Poker playing program v0.0
  40. >// by N A Llewellyn
  41. >
  42. >#include <iostream.h>
  43. >#include <classlib\arrays.h>
  44. >
  45. >// in order of rank from low to high
  46. >enum RANK
  47. >{two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace};
  48. >
  49. >enum SUIT {clubs,harts,diamonds,spades}; // no order of rank
  50. >
  51. >enum FACE {up,down};
  52. >
  53. >class Card {
  54. >
  55. >    private:
  56. >        RANK rank;
  57. >        SUIT suit;
  58. >        FACE face;
  59. >
  60. >    friend ostream& operator << (ostream&,const Card&);
  61. >
  62. >    public:
  63. >        Card() {rank=two;suit=clubs;face=down;}
  64. >        Card(const Card & c)
  65. {rank=c.rank;suit=c.suit;face=c.face;}
  66. >        int operator == (const Card & c) {return rank == c.rank;}
  67. >        int operator < (const Card & c) {return rank < c.rank;}
  68. >        void SetRank(RANK r) {rank=r;}
  69. >        void SetSuit(SUIT s) {suit=s;}
  70. >        void SetFace(FACE f) {face=f;}
  71. >};
  72. >
  73. >typedef TSArrayAsVector <Card> CardArray;
  74. >typedef TSArrayAsVectorIterator <Card> CardArrayIterator;
  75. >
  76. >class Pack {
  77. >
  78. >    private:
  79. >        CardArray pack;
  80. >        unsigned int NumberOfCards;
  81. >
  82. >    public:
  83. >        Pack();
  84. >        void AddCard(Card c) {pack.Add(c);NumberOfCards++;}
  85. >        void FindAndDetach(Card c) {pack.Detach(c);}
  86. >        void DetachFrom(int i) {pack.Detach(i);}
  87. >        void FlushPack(void) {pack.Flush();NumberOfCards=0;}
  88. >        int IsPackEmpty(void) {return pack.IsEmpty();}
  89. >};
  90. >
  91. >class Hand {
  92. >
  93. >    private:
  94. >        CardArray hand;
  95. >        unsigned int NumberOfCards;
  96. >
  97. >    public:
  98. >        Hand();
  99. >        void AddCard(Card c) {hand.Add(c);NumberOfCards++;}
  100. >        void FindAndDetach(Card c) {hand.Detach(c);}
  101. >        void DetachFrom(int i) {hand.Detach(i);}
  102. >        void FlushPack(void) {hand.Flush();NumberOfCards=0;}
  103. >        int IsPackEmpty(void) {return hand.IsEmpty();}
  104. >        ~Hand();
  105. >};
  106. >
  107. >ostream& operator << (ostream& os,const Card& c)
  108. >    {return os<<c.rank<<","<<c.suit<<","<<c.face;}
  109. >
  110. >// definition of constructor for Pack
  111. >
  112. >Pack::Pack()
  113. >{
  114. >    Card card;
  115. >    SUIT suit;
  116. >    RANK rank;
  117. >
  118. >    NumberOfCards=0;
  119. >
  120. >    for(suit=clubs;suit<=spades;suit++)
  121. >        for(rank=two;rank<=ace;rank++)
  122. >            {
  123. >                card.SetSuit(suit);
  124. >                card.SetRank(rank);
  125. >                card.SetFace(down);
  126. >                AddCard(card);
  127. >                NumberOfCards++;
  128. >            }
  129. >}
  130. >
  131. >void Show(Card& c,void*)
  132. >    {cout<<c<<endl;}
  133. >
  134. >void UseForEach(CardArray& c)
  135. >    {c.ForEach(Show,0);}
  136. >
  137. >int main()
  138. >{
  139. >    Pack pack1;
  140. >    UseForEach(pack1.pack);
  141. >    return 0;
  142. >}
  143. >
  144.  
  145. You're running into this problem because Borland's TArrayAsVector class
  146. does not have a default constructor.  For instance, when your Pack
  147. class gets constructed, one of the first things the compiler does is
  148. search for the constructor for each data member of the class. Since you
  149. typedef'd your CardArray as a TArray, the compiler will search for
  150. TArray's default constructor.  Since it doesn't have one, you get this
  151. error message.
  152.  
  153. The way to solve it is to include your TArray in the initialization
  154. list for the constructor of your Pack (and Hand) class. The code would
  155. look like this:
  156.  
  157. ----------
  158. Pack::Pack() : pack(n1,n2,n3)
  159. {
  160.   //your constructor code
  161. }
  162. ----------
  163.  
  164. Since your data member, pack, is a CardArray, it needs to have its
  165. constructor specified in the initialization list, which resides after
  166. the single colon.  The values n1, n2, and n3 are defined as:
  167.  
  168. n1:  Initial size of the array
  169. n2:  Initial start point of the array (usually 0)
  170. n3:  Number of extra array members to dynamically add if the array
  171. grows beyond its inital bound.
  172.  
  173. Hope this helps.
  174.  
  175. bob
  176. nanninr@ix.netcom.com
  177.